package com.yc.springmvc.config;

import com.yc.springmvc.LoginInterceptor;
import com.yc.springmvc.SqlDateConvertor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Date;

@Configuration
@EnableWebMvc
@ComponentScan("com.yc.springmvc.controller")
public class MvcConfig implements WebMvcConfigurer {

    //静态资源映射 (html,css,js)
   /* @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
*/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //file:：表示指定的资源是文件系统中的文件，而不是通过类路径或网络等其他方式获取。
        registry.addResourceHandler("/**").addResourceLocations("file:D:\\java\\git\\git local\\s3\\SpringMvc\\src\\main\\webapp/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:D:\\java\\git\\git local\\s3\\SpringMvc\\src\\main\\webapp/");
    }

    //实现文件上传配置
    @Bean
    public MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.removeConvertible(String.class, Date.class);
        registry.addConverter(new SqlDateConvertor());
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns("/A.html")    需要拦截的地址
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/A.html");
    }
}
